<?php

/**
 * TaskWall actions.
 *
 * @package    time-tracker
 * @subpackage TaskWall
 * @author     Carlo Di Fulco
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class TaskWallActions extends sfActions
{
	/**
	 *
	 * @extdirect-enable
	 */
	public function executeGetTasks(){

		$data = Doctrine_Core::getTable('TtTask')->findBy('is_show',1); 
		$this->result = TtTask::convertCollectionToArray($data);
		return sfView::SUCCESS;

	}

	/**
	 *
	 * @extdirect-enable
	 *
	 */
	public function executeGetProjects()
	{
		$data = Doctrine_Core::getTable('TtProject')->findAll();
		$this->result = TtProject::convertCollectionToArray($data);
		return sfView::SUCCESS;
	}


	/**
	 *
	 * @extdirect-enable
	 * @extdirect-len 1
	 *
	 */
	public function executeStartTask()
	{
		
		if (!$pending = TtTaskTable::findPendings()){
			
			$taskId = $this->getRequestParameter('tid');
			$task = Doctrine_Core::getTable('TtTask')->find($taskId);
	
			$success = $task->start() ? true : false;
			
			$this->result = array('success' => $success);
			
		} else {
			
			$this->result = array(
				'success'	=> false, 
				'msg'	 	=> 'Another task is already running'
			);
			
		}
		
	}

	/**
	 *
	 * @extdirect-enable
	 * @extdirect-len 1
	 *
	 */
	public function executeStopTask()
	{

		if($pending = TtTaskTable::findPendings()){
			
			$taskId = $this->getRequestParameter('tid');
			$task = Doctrine_Core::getTable('TtTask')->find($taskId);
			
			if($taskId == $pending->id){
				$success = $task->stop() ? true : false;
				$this->result = array('success' => $success);
			} else {
				
				$this->result = array(
					'success' 	=> false,
					'msg'		=> 'Task "' . $task->name . '" is not running'
				);
			
			}
			
		} else {
			
			$this->result = array(
				'success' 	=> false,
				'msg'		=> 'No task is running'
			);
		
		}
		
		return sfView::SUCCESS;
	}

  
	/**
	 *
	 * @extdirect-enable
	 * @extdirect-len 1
	 *
	 */
	public function executeCloseTask()
	{
		try {
		
			$taskId = $this->getRequestParameter('tid');
			$task = Doctrine_Core::getTable('TtTask')->find($taskId);
	
			$success = $task->close();
			
			if ($success){
				$this->result = array('success' => $success);	
			} else {
				$this->result = array(
					'success' 	=> $success, 
					'msg' 		=> 'The task is already closed'
				);
			}
			
		} catch (Exception $e) {
			
			$this->result = array('success' => false, 'msg' => $e->getMessage());
			
		}
				
		return sfView::SUCCESS;
		
	}

}
